import request from "@/router/axios";
import { baseUrl } from "@/config/env";

export const getkindList = (data) =>
  request({
    url: baseUrl + "/api/query/facility",
    method: "get",
    params: data,
  });

export const gitsop = (data) =>
  request({
    url: baseUrl + "/api/sops",
    method: "get",
    params: data,
  });

  export const Eitputdata = (list) => request({
    url: baseUrl + '/api/query/component',
    method: 'put',
    data: list
  });

  export const eitsop = (list) => request({
    url: baseUrl + '/api/sops',
    method: 'put',
    data: list
  });
export const getdstation = (data) =>
  request({
    url: baseUrl + "/api/query/station/details",
    method: "get",
    params: data,
  });

export const getdkind = (data) =>
  request({
    url: baseUrl + "/api/query/aircraft/kind",
    method: "get",
    params: data,
  });

  export const getStation = (query) => {
    const url = new URL('http://192.168.20.252:8080/api/sops/station');
    Object.keys(query).forEach(key => {
      if (query[key] !== null && query[key] !== undefined) {
        url.searchParams.append(key, query[key]);
      }
    });

    return fetch(url.toString(), {
      method: 'GET',
      headers: {
        'Accept': 'application/pdf',
        'Cache-Control': 'no-cache'
      }
    }).then(response => {
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
      return response.arrayBuffer();
    }).then(arrayBuffer => {
      console.log('✅ 接收到原始二进制数据，大小:', arrayBuffer.byteLength);
      return { data: arrayBuffer };
    });
  };

export const getdTaskkind = (data) =>
  request({
    url: baseUrl + "/api/query/task/kind",
    method: "get",
    params: data,
  });

export const delsop = (data) =>
  request({
    url: baseUrl + "/api/sops",
    method: "delete",
    data: data,
    headers: {
      'Content-Type': 'application/json'
    }
  });

  export const addsop = (list) =>
    request({
      url: baseUrl + "/api/sops",
      method: "post",
      data: list,
    });

export const logout = () =>
  request({
    url: baseUrl + "/user/logout",
    meta: {
      isToken: false,
    },
    method: "get",
  });
export const getCpk = (list) =>
  request({
    url: baseUrl + "/api/calculate/cpk",
    method: "post",
    data: list,
  });
